﻿using System.Linq;
using System.Text;
using System.Globalization;

namespace DadaLib
{
    /// <summary>
    /// Creates sentences getting input from a Generator
    /// </summary>
    public class Composer
    {
        private readonly IGenerator _generator;

        /// <summary>
        /// Initializes a new instance of the <see cref="Composer"/> class.
        /// </summary>
        /// <param name="generator">The generator.</param>
        public Composer(IGenerator generator)
        {
            this._generator = generator;            
        }


        /// <summary>
        /// Composes a sentence finishing with period.
        /// </summary>
        /// <param name="numWords">The number of words.</param>
        /// <returns>A sentence with an undetermined number of words.</returns>
        public string ComposeSentence(int numWords)
        {
            var sb = new StringBuilder();
            string word = _generator.SearchStartWord();

            for (int i = 0; i < numWords; i++)
            {
                if (!IsPunctuation(word) && (i != 0))
                    sb.Append(" ");
                sb.Append(word);
                word = _generator.GetNextWord(word);
            }
            sb.AppendLine();
            return sb.ToString();
        }

        /// <summary>
        /// Composes a sentence finishing with 'period'.
        /// </summary>
        /// <returns>A sentence finishing with period.</returns>
        public string ComposeSentence()
        {
            var sb = new StringBuilder();
            string word = _generator.SearchStartWord();
            int i = 0;
            do
            {
                if (!IsPunctuation(word) && (i != 0))
                    sb.Append(" ");
                sb.Append(word);
                word = _generator.GetNextWord(word);
                i++;
            } while (word != ".");

            sb.AppendLine(".");
            return sb.ToString();
        }

        /// <summary>
        /// Words considered as punctuation
        /// </summary>
        public static string[] PunctuationWords = new[] { ",", ".", ";", "\"", "'", "¿", "?", "!", "¡", ":", "-" };
        public static bool IsPunctuation(string word)
        {
            return PunctuationWords.Contains(word);
        }

        /// <summary>
        /// Capitalizes the specified word.
        /// </summary>
        /// <param name="word">The input word.</param>
        /// <returns>The capitalized word.</returns>
        public string Capitalize(string word)
        {
            return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word);
        }

    }
}
